/**
 * 智慧小区管理系统 - 投诉建议模块API接口
 * @author Wu.Liang
 * @date 2025-06-26
 * @version 3.0 - 根据系统设计调整方案重构，独立投诉建议模块
 */

import api from '@/api/request'
import type {
  ApiResponse,
  PageResponse,
  ComplaintQueryParams,
  Complaint,
  ComplaintCategory,
  ComplaintProcess,
  ComplaintFollowup,
  ComplaintStatistics,
  ComplaintKnowledge,
  CreateComplaintParams,
  UpdateComplaintParams,
  AssignComplaintParams,
  ProcessComplaintParams,
  FollowupComplaintParams,
  SatisfactionParams,
  BatchComplaintParams
} from '@/views/complaint/types/complaint'
import type { Community, Building, Household } from '@/views/property/types/property'

// 投诉建议管理API - 根据新接口调整
export const complaintApi = {
  // 分页查询投诉建议 - 新接口路径
  getComplaintList: (params: ComplaintQueryParams) => {
    return api.get<ApiResponse<PageResponse<Complaint>>>('/api/complaint/page', { params })
  },

  // 获取投诉建议详情 - 新接口路径
  getComplaintDetail: (complaintId: number) => {
    return api.get<ApiResponse<Complaint>>(`/api/complaint/${complaintId}`)
  },

  // 创建投诉建议 - 新接口路径
  createComplaint: (data: CreateComplaintParams) => {
    return api.post<ApiResponse<{ complaintId: number }>>('/api/complaint/submit', data)
  },

  // 更新投诉建议 - 新接口路径
  updateComplaint: (complaintId: number, data: UpdateComplaintParams) => {
    return api.put<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}`, data)
  },

  // 删除投诉建议 - 新接口路径
  deleteComplaint: (complaintId: number) => {
    return api.delete<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}`)
  },

  // 分配投诉建议 - 新接口路径
  assignComplaint: (complaintId: number, data: AssignComplaintParams) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/assign`, data)
  },

  // 处理投诉建议 - 新接口路径
  processComplaint: (complaintId: number, data: ProcessComplaintParams) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/process`, data)
  },

  // 回访投诉建议 - 新接口路径
  followupComplaint: (complaintId: number, data: FollowupComplaintParams) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/followup`, data)
  },

  // 满意度评价 - 新接口路径
  submitSatisfaction: (complaintId: number, data: SatisfactionParams) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/satisfaction`, data)
  },

  // 批量操作 - 新接口路径
  batchProcessComplaints: (data: BatchComplaintParams) => {
    return api.post<ApiResponse<{ successCount: number; totalCount: number }>>('/api/complaint/batch-process', data)
  },

  // 导出投诉建议 - 新接口路径
  exportComplaints: (params: ComplaintQueryParams) => {
    return api.get('/api/complaint/export', { 
      params,
      responseType: 'blob'
    })
  },

  // 获取投诉统计信息 - 新接口路径
  getComplaintStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<ComplaintStatistics>>('/api/complaint/statistics', { params })
  },

  // 投诉重开 - 新接口路径
  reopenComplaint: (complaintId: number, reason: string) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/reopen`, null, {
      params: { reason }
    })
  },

  // 关闭投诉 - 新接口路径
  closeComplaint: (complaintId: number, remark: string) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/close`, null, {
      params: { remark }
    })
  },

  // 解决投诉 - 新接口路径
  solveComplaint: (complaintId: number, solution: string) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/${complaintId}/solve`, null, {
      params: { solution }
    })
  }
}

// 投诉分类管理API - 保持原有接口，但路径调整
export const complaintCategoryApi = {
  // 获取投诉分类列表
  getCategoryList: (params?: { isEnabled?: boolean }) => {
    return api.get<ApiResponse<ComplaintCategory[]>>('/api/complaint/categories', { params })
  },

  // 获取投诉分类详情
  getCategoryDetail: (categoryId: number) => {
    return api.get<ApiResponse<ComplaintCategory>>(`/api/complaint/categories/${categoryId}`)
  },

  // 创建投诉分类
  createCategory: (data: Partial<ComplaintCategory>) => {
    return api.post<ApiResponse<{ categoryId: number }>>('/api/complaint/categories', data)
  },

  // 更新投诉分类
  updateCategory: (categoryId: number, data: Partial<ComplaintCategory>) => {
    return api.put<ApiResponse<{ success: boolean }>>(`/api/complaint/categories/${categoryId}`, data)
  },

  // 删除投诉分类
  deleteCategory: (categoryId: number) => {
    return api.delete<ApiResponse<{ success: boolean }>>(`/api/complaint/categories/${categoryId}`)
  },

  // 启用/禁用投诉分类
  toggleCategoryStatus: (categoryId: number, isEnabled: boolean) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/categories/${categoryId}/toggle`, { isEnabled })
  }
}

// 投诉处理流程API - 保持原有接口，但路径调整
export const complaintProcessApi = {
  // 获取投诉处理流程
  getProcessList: (complaintId: number) => {
    return api.get<ApiResponse<ComplaintProcess[]>>(`/api/complaint/processes/${complaintId}`)
  },

  // 获取处理流程详情
  getProcessDetail: (processId: number) => {
    return api.get<ApiResponse<ComplaintProcess>>(`/api/complaint/processes/detail/${processId}`)
  }
}

// 投诉回访记录API - 保持原有接口，但路径调整
export const complaintFollowupApi = {
  // 获取投诉回访记录
  getFollowupList: (complaintId: number) => {
    return api.get<ApiResponse<ComplaintFollowup[]>>(`/api/complaint/followups/${complaintId}`)
  },

  // 获取回访记录详情
  getFollowupDetail: (followupId: number) => {
    return api.get<ApiResponse<ComplaintFollowup>>(`/api/complaint/followups/detail/${followupId}`)
  }
}

// 投诉统计分析API - 保持原有接口，但路径调整
export const complaintStatisticsApi = {
  // 获取投诉统计信息
  getStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<ComplaintStatistics>>('/api/complaint/statistics', { params })
  },

  // 获取分类统计
  getCategoryStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<Array<{ categoryId: number; categoryName: string; count: number; percentage: number }>>>('/api/complaint/statistics/category', { params })
  },

  // 获取月度统计
  getMonthlyStatistics: (params?: { communityId?: number; year?: number }) => {
    return api.get<ApiResponse<Array<{ month: string; count: number; resolvedCount: number }>>>('/api/complaint/statistics/monthly', { params })
  },

  // 获取处理人员统计
  getAssigneeStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<Array<{ assigneeId: number; assigneeName: string; count: number; avgResolveTime: number }>>>('/api/complaint/statistics/assignee', { params })
  }
}

// 投诉知识库API - 保持原有接口，但路径调整
export const complaintKnowledgeApi = {
  // 分页查询知识库
  getKnowledgeList: (params: { current?: number; size?: number; categoryId?: number; keyword?: string }) => {
    return api.get<ApiResponse<PageResponse<ComplaintKnowledge>>>('/api/complaint/knowledge', { params })
  },

  // 获取知识库详情
  getKnowledgeDetail: (knowledgeId: number) => {
    return api.get<ApiResponse<ComplaintKnowledge>>(`/api/complaint/knowledge/${knowledgeId}`)
  },

  // 创建知识库条目
  createKnowledge: (data: Partial<ComplaintKnowledge>) => {
    return api.post<ApiResponse<{ knowledgeId: number }>>('/api/complaint/knowledge', data)
  },

  // 更新知识库条目
  updateKnowledge: (knowledgeId: number, data: Partial<ComplaintKnowledge>) => {
    return api.put<ApiResponse<{ success: boolean }>>(`/api/complaint/knowledge/${knowledgeId}`, data)
  },

  // 删除知识库条目
  deleteKnowledge: (knowledgeId: number) => {
    return api.delete<ApiResponse<{ success: boolean }>>(`/api/complaint/knowledge/${knowledgeId}`)
  },

  // 增加浏览量
  incrementViewCount: (knowledgeId: number) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/complaint/knowledge/${knowledgeId}/view`)
  }
}

// 文件上传API
export const complaintFileApi = {
  // 上传图片
  uploadImage: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('type', 'image')
    return api.post<ApiResponse<{ url: string }>>('/api/complaint/upload/image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 上传附件
  uploadAttachment: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('type', 'attachment')
    return api.post<ApiResponse<{ url: string; fileName: string }>>('/api/complaint/upload/attachment', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

// 通用API - 保持原有接口
export const commonApi = {
  // 获取小区列表
  getCommunityList: () => {
    return api.get<ApiResponse<Community[]>>('/api/region/communities')
  },

  // 获取楼栋列表
  getBuildingList: (params: { communityId: number }) => {
    return api.get<ApiResponse<Building[]>>('/api/region/buildings', { params })
  },

  // 获取房户列表
  getHouseholdList: (params: { communityId?: number; buildingId?: number }) => {
    return api.get<ApiResponse<Household[]>>('/api/region/households', { params })
  },

  // 获取用户列表
  getUserList: (params: { communityId?: number; keyword?: string }) => {
    return api.get<ApiResponse<any[]>>('/api/user/list', { params })
  }
} 